package com.lxmes.hr.service.impl;

import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcPersonnelInfoMapper;
import com.lxmes.hr.domain.NcPersonnelInfo;
import com.lxmes.hr.service.INcPersonnelInfoService;

import javax.validation.Validator;

/**
 * 人事档案Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-24
 */
@Service
public class NcPersonnelInfoServiceImpl implements INcPersonnelInfoService
{
    @Autowired
    private NcPersonnelInfoMapper ncPersonnelInfoMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询人事档案
     *
     * @param id 人事档案主键
     * @return 人事档案
     */
    @Override
    public NcPersonnelInfo selectNcPersonnelInfoById(Long id)
    {
        return ncPersonnelInfoMapper.selectNcPersonnelInfoById(id);
    }

    /**
     * 查询人事档案列表
     *
     * @param ncPersonnelInfo 人事档案
     * @return 人事档案
     */
    @Override
    public List<NcPersonnelInfo> selectNcPersonnelInfoList(NcPersonnelInfo ncPersonnelInfo)
    {
        return ncPersonnelInfoMapper.selectNcPersonnelInfoList(ncPersonnelInfo);
    }

    /**
     * 新增人事档案
     *
     * @param ncPersonnelInfo 人事档案
     * @return 结果
     */
    @Override
    public int insertNcPersonnelInfo(NcPersonnelInfo ncPersonnelInfo)
    {
        return ncPersonnelInfoMapper.insertNcPersonnelInfo(ncPersonnelInfo);
    }

    /**
     * 修改人事档案
     *
     * @param ncPersonnelInfo 人事档案
     * @return 结果
     */
    @Override
    public int updateNcPersonnelInfo(NcPersonnelInfo ncPersonnelInfo)
    {
        return ncPersonnelInfoMapper.updateNcPersonnelInfo(ncPersonnelInfo);
    }

    /**
     * 批量删除人事档案
     *
     * @param ids 需要删除的人事档案主键
     * @return 结果
     */
    @Override
    public int deleteNcPersonnelInfoByIds(Long[] ids)
    {
        return ncPersonnelInfoMapper.deleteNcPersonnelInfoByIds(ids);
    }

    /**
     * 删除人事档案信息
     *
     * @param id 人事档案主键
     * @return 结果
     */
    @Override
    public int deleteNcPersonnelInfoById(Long id)
    {
        return ncPersonnelInfoMapper.deleteNcPersonnelInfoById(id);
    }

    @Override
    public String imporIteminfo(List<NcPersonnelInfo> personnelInfos, boolean updateSupport, String operName) {
        if (StringUtils.isNull(personnelInfos) || personnelInfos.size() == 0)
        {
            throw new ServiceException("导入物料数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcPersonnelInfo personnelInfo : personnelInfos)
        {
            try
            {
                BeanValidators.validateWithException(validator, personnelInfo);
                personnelInfo.setCreateBy(operName);
                personnelInfo.setFactoryCode("WR");
                personnelInfo.setFactoryName("玮瑞");
                personnelInfo.setUpdateBy(operName);
                this.insertNcPersonnelInfo(personnelInfo);
                successNum++;
                successMsg.append("<br/>" + successNum + "、人事 " + personnelInfo.getPName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + personnelInfo.getPName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
